# -*- coding:utf-8 -*-
from datetime import datetime
import re

from django.db import models
from django.utils.html import strip_tags
from ckeditor_uploader.fields import RichTextUploadingField

from users.models import UserProfile


class Topic(models.Model):
    title = models.CharField(u'标题', max_length=50)
    user = models.ForeignKey(UserProfile, verbose_name=u'用户')
    desc = models.CharField(u'摘要', max_length=200, blank=True)
    content = RichTextUploadingField(u'内容')
    like_number = models.IntegerField(u'喜欢人数', default=0)
    comment_number = models.IntegerField(u'评论人数', default=0)
    click_number = models.IntegerField(u'点击量', default=0)
    add_time = models.DateTimeField(u'发帖时间', default=datetime.now)

    def save(self, *args, **kwargs):
        if not self.desc:
            self.desc = strip_tags(self.content[:200])
        super(Topic, self).save(*args, **kwargs)

    class Meta:
        verbose_name = u'话题帖信息'
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.title


class TopicReply(models.Model):
    user = models.ForeignKey(UserProfile, verbose_name=u'用户')
    topic = models.ForeignKey(Topic, verbose_name=u'主题帖')
    content = RichTextUploadingField(u'内容')
    add_time = models.DateTimeField(u'回帖时间', default=datetime.now)

    class Meta:
        verbose_name = u'回复帖信息'
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.topic.title


class CommunityBanner(models.Model):
    title = models.CharField(u'标题', max_length=100)
    image = models.ImageField(u'轮播图', upload_to='banner/%Y/%m', blank=True)
    url = models.URLField(u'访问地址', max_length=200)
    index = models.IntegerField(u'顺序', default=100)
    add_time = models.DateTimeField(u'添加时间', default=datetime.now)

    class Meta:
        verbose_name = u'轮播图'
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.title
